<?php
/**
 * Created by PhpStorm.
 * User: 火柴
 * Date: 2016/4/18
 * GitHub: https://github.com/huaiguoguo
 * Email: 290559038@qq.com
 * Time: 23:00
 */

namespace backend\controllers;

use yii;
use yii\helpers\Json;
use yii\base\Exception;
use common\helper\Dump;
use common\extend\EController;
use common\models\Goods;

use yii\web\UploadedFile;
use common\models\UploadForm;


class GoodsController extends EController
{




    public function actionIndex()
    {
        $data          = [];
        $data['goods'] = Goods::find()->all();

        return $this->render('index', $data);
    }

    public function actionAdd()
    {
        $data       = [];
        $GoodsModel = new Goods();
        if (Yii::$app->request->post()) {
            $goods = Yii::$app->request->post();
            if ($GoodsModel->load($goods) && $GoodsModel->validate()) {
                if ($GoodsModel->save()) {
                    return self::success('新增商品成功', 'index');
                } else {
                    return self::error('新增商品失败', 'add');
                }
            }
        }

        $data['Goods']                = $GoodsModel;
        $data['green_space']          = [
            ['id' => 1, 'name' => '大厅'],
            ['id' => 2, 'name' => '前台'],
            ['id' => 3, 'name' => '总裁室'],
            ['id' => 4, 'name' => '会议室']
        ];
        $data['green_function']       = [
            ['id' => 1, 'name' => '美观'],
            ['id' => 2, 'name' => '净化空气'],
            ['id' => 3, 'name' => '吸甲醛'],
            ['id' => 4, 'name' => '趣味']
        ];
        $data['green_specifications'] = [
            ['id' => 1, 'name' => '100*100'],
            ['id' => 2, 'name' => '200*200'],
            ['id' => 3, 'name' => '300*300'],
            ['id' => 4, 'name' => '400*400']
        ];

        $data['form'] = yii\widgets\ActiveForm::begin([
                                                          'options'            => ['class' => 'form-horizontal'],
                                                          'enableClientScript' => '',
                                                          'method'             => 'post',
                                                          'fieldConfig'        => [
                                                              'template'     => "{label}\n<div class=\"col-sm-10\">{input}</div>\n<span class='has-error'>{error}</span>",
                                                              'labelOptions' => ['class' => 'col-sm-2 control-label'],
                                                          ]
                                                      ]);

        return $this->render('add', $data);
    }


    public function actionEdit()
    {
        $data       = [];
        $id         = Yii::$app->request->get('id');
        $GoodsModel = Goods::findOne($id);
        if (!$GoodsModel) {
            return self::error('非法操作', 'index');
        }

        $data['green_space'] = [
            ['id' => 1, 'name' => '大厅'],
            ['id' => 2, 'name' => '前台'],
            ['id' => 3, 'name' => '总裁室'],
            ['id' => 4, 'name' => '会议室']
        ];

        $data['green_function'] = [
            ['id' => 1, 'name' => '美观'],
            ['id' => 2, 'name' => '净化空气'],
            ['id' => 3, 'name' => '吸甲醛'],
            ['id' => 4, 'name' => '趣味']
        ];

        $data['green_specifications'] = [
            ['id' => 1, 'name' => '100*100'],
            ['id' => 2, 'name' => '200*200'],
            ['id' => 3, 'name' => '300*300'],
            ['id' => 4, 'name' => '400*400']
        ];

        if (Yii::$app->request->post()) {
            if ($GoodsModel->load(Yii::$app->request->post()) && $GoodsModel->validate()) {
                if ($GoodsModel->update()) {
                    return self::success('更新成功', 'index');
                } else if (empty($GoodsModel->getErrors())) {
                    return self::error('记录没有改动过', ['edit', 'id' => $id]);
                } else {
                    return self::error('更新失败', ['edit', 'id' => $id]);
                }
            }
        }


        $data['Goods'] = $GoodsModel;

        $data['form'] = yii\widgets\ActiveForm::begin([
                                                          'options'            => ['class' => 'form-horizontal'],
                                                          'enableClientScript' => '',
                                                          'method'             => 'post',
                                                          'fieldConfig'        => [
                                                              'template'     => "{label}\n<div class=\"col-sm-10\">{input}</div>\n<span class='has-error'>{error}</span>",
                                                              'labelOptions' => ['class' => 'col-sm-2 control-label'],
                                                          ]
                                                      ]);

        return $this->render('edit', $data);
    }


    public function actionDel()
    {
        if (Yii::$app->request->isAjax && Yii::$app->request->post()) {
            $id  = Yii::$app->request->post('id');
            $res = Goods::findOne($id);
            if (!$res) return Json::encode($this->makeinformation(505, '非法操作'));
            if ($res->delete()) {
                $json = Json::encode($this->makeinformation(200, '删除成功'));
            } else {
                $json = Json::encode($this->makeinformation(505, '删除失败'));
            }

            return $json;
        }
    }


    //上传文件
    public function actionUpload()
    {
        if (Yii::$app->request->isAjax && Yii::$app->request->post()) {
            $model       = new UploadForm();
            $model->file = UploadedFile::getInstanceByName('images');
            if ($model->file && $model->validate()) {
                $dir = $this->checkDir('./upload/');
                $model->file->saveAs($dir . $model->file->baseName . '.' . $model->file->extension);

                return $this->ajaxReturn($this->makeinformation(200, '', $model->file->name));
            } else {
                return $this->ajaxReturn($model->getErrors());
            }
        } else {
            return $this->ajaxReturn($this->makeinformation(505, '错误'));
        }
    }


}